<template>
  <div class="w">

    <!-- 表单区 -->
    <Form style="padding-top: 35px;" 
      name="form" 
      ref="formRef" 
      :labelCol="{ span: 6 }" 
      :wrapperCol="{ span: 18 }" 
      :initialValues="form"
      @finish="formFinish">

      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="resourceNumber" label="资源编号">
            <Input style="width: 100%" disabled />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="resourceName" label="资源名称" :rules="formRules.resourceName">
            <Input placeholder="请输入资源名称" style="width: 100%" />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="resourceType" label="资源类型" :rules="formRules.resourceType" style="position: relative;" id="selectPosition">
            <Select placeholder="请选择资源类型" style="width: 100%" allowClear :getPopupContainer="getPopupContainer">
              <Option v-for="type in refData.resourceTypes" :key="type.value" :value="type.value" :label="type.label" />
            </Select>
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="fatoryName" label="厂家名称" :rules="formRules.fatoryName">
            <Input placeholder="请输入厂家名称" style="width: 100%" />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="ipAddress" label="IP地址">
            <Input placeholder="请输入IP地址" style="width: 100%" />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="isInUse" label="是否使用中">
            <Checkbox disabled />
          </FormItem>
        </Col>
      </Row>
      <Row>
        <Col :span="20" :offset="0">
          <FormItem name="remark" label="备注">
            <TextArea placeholder="请输入备注信息" style="width: 100%; height: 200px;" />
          </FormItem>
        </Col>
      </Row>
      <Row style="margin-top: 20px;">
        <Col :span="12" :offset="6">
          <FormItem :wrapperCol="{ span: 24 }" style="text-align: center">
            <Button style="width: 100px;" type="primary" @click="formValidate">提 交</Button>
            <Button style="width: 100px; margin-left: 20px;" @click="formCancel">取 消</Button>
          </FormItem>
        </Col>
      </Row>
    </Form>

    <!-- 提交确认框 -->
    <Modal title="提交确认" v-model="isModalVisible" @ok="formSubmit" @cancel="isModalVisible = false" centered>
      <p>即将提交硬件资源信息，是否确认？</p>
    </Modal>
  </div>
</template>

<script>
import * as ruoyi from '@/utils/ruoyi'
import { Message } from '@uni/vue';
import { addResource, updateResource } from "@/api/lampstand/resource";

export default {
  name: "app",
  props: {
    resourceId: {
      type: Number,
      default: 0
    },
    currRow: {
      default: () => {}
    },
    refData: {
      default: () => {}
    }
  },
  data() {
    const validateResourceType = (rule, value) => {
      let type = this.refData.resourceTypes.find(type => type.value == value);
      if (type) return Promise.resolve();
      else return Promise.reject("请选择资源类型");
    };
    return {
      form: {
        resourceNumber: this.resourceId ? this.currRow.resourceNumber : `R${this.getCurrentTime()}`,
        resourceName: this.currRow.resourceName,
        resourceType: `${this.currRow.resourceType ? this.currRow.resourceType : ''}`,
        fatoryName: this.currRow.fatoryName,
        ipAddress: this.currRow.ipAddress,
        isInUse: this.currRow.isInUse,
        remark: this.currRow.remark
      },
      formRules: {
        resourceName: [
          { required: true, message: '请输入资源名称' },
        ],
        resourceType: [
          // { required: true, message: '请选择资源类型' },
          { required: true, validator: validateResourceType },
        ],
        fatoryName: [
          { required: true, message: '请输入厂家名称' },
        ]
      },
      isModalVisible: false
    }
  },
  mounted: function () {
    // console.log("mounted resourceId: ", this.resourceId);
    // console.log("mounted currRow: ", this.currRow);
    // console.log("mounted refData: ", this.refData);
  },
  methods: {
    getPopupContainer() {
      return document.querySelector("#selectPosition")
    },
    formValidate: function () {
      this.$refs.formRef.submit();
    },
    formCancel: function () {
      // this.$refs.formRef.reset();
      this.handleCancel();
    },
    formFinish: function (formValues) {
      this.form = formValues;
      // this.isModalVisible = true;
      this.formSubmit();
    },
    formSubmit: async function () {
      // 公共属性
      this.form.createBy = this.$store.state.user.name;
      this.form.createTime = ruoyi.parseTime(Date.now());
      this.form.updateBy = this.$store.state.user.name;
      this.form.updateTime = ruoyi.parseTime(Date.now());

      // 提交表单请求
      this.isModalVisible = false;
      let reqForm = JSON.parse(JSON.stringify(this.form));
      // console.log("reqForm: ", reqForm);

      if (this.resourceId) {
        // 修改
        reqForm.resourceId = this.resourceId;
        const rs = await updateResource(reqForm);
        if (rs.code !== 200) return Message.error({ text: '修改硬件资源失败：' + rs.msg });
        Message.success({ text: '修改硬件资源完成' });
        this.handleCancel();

      } else {
        // 新增
        const rs = await addResource(reqForm);
        if (rs.code !== 200) return Message.error({ text: '新增硬件资源失败：' + rs.msg });
        Message.success({ text: '新增硬件资源完成' });
        this.handleCancel();
      }
    },
    handleCancel: function () {
      this.$emit('handleCancel');
    }
  }
}
</script>

<style scoped>
  .w {
    background-color: #F9FAFB;
  }

  /* 标题 */
  .title-area span {
    display: inline-block;
    font-weight: 500;
    line-height: 36px;
    margin-left: 15px;
  }
</style>
